package com.aishua.fire.jpa;


import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.transaction.annotation.Transactional;

import com.aishua.fire.jpa.entity.Account;
import java.lang.Long;
import java.util.List;

/**
 * jpa使用
 * @author Administrator
 *
 */
public interface AccountDao extends JpaRepository<Account, Long>,JpaSpecificationExecutor<Account> {
	//根据userName查询用户id
	 @Transactional
	 @Query(value = "select login_id from account where user_name = ?1 and status=0",nativeQuery=true)
	 Long findLoginIdByUserName(String userName);
	 
	 @Transactional
	 @Modifying
	 @Query(value = "SELECT\r\n" + 
	 		"  c.*\r\n" + 
	 		"FROM\r\n" + 
	 		"  account_accountteam ab,\r\n" + 
	 		"  account c\r\n" + 
	 		"WHERE c.login_id = ab.account_id\r\n" + 
	 		"  AND ab.accountteam_id = ?1\r\n" + 
	 		"AND c.status=0 AND c.login_id = ?2\\r\\n\"  AND c.user_name LIKE CONCAT('%',:userName,'%')",nativeQuery=true)
	 List<Account> findAccountByaccountteamId(Long accountteamId,Long loginId,String userName);
	 
	//修改密码user_password
	 @Transactional
	 @Modifying
	 @Query(value = "update account set user_password=?1 where login_id=?2",nativeQuery=true)
	 Integer modifypwd(String password,String loginId);

	 @Transactional
	 @Query(value = "select t from Account t where t.userName=?1")
	 Account getByUserName(String userName);
	 
	//禁止账户
	 @Transactional
	 @Modifying
	 @Query(value = "update account set account_status=?1 where login_id=?2",nativeQuery=true)
	 void UpdateAccountStatus(String accountStatus,String loginId);

	 //修改账户
	 @Transactional
	 @Modifying
	 @Query(value = "update account set user_name=?1,email_addr=?2,alias_name=?3,phone=?4 where login_id=?5",nativeQuery=true)
	 void updateAccount(String userName,String emailAddr,String aliasName,String phone,String loginId);

	//软删除账户
	 @Transactional
	 @Modifying
	 @Query(value = "update account set status=?1 where login_id=?2",nativeQuery=true)
	 void softDeleteAccount(String status,String loginId);
	 
	 @Transactional
	 @Query(value = "select t from Account t where t.userName =?1 and t.userPassword = ?2 and t.accountStatus = ?3")
	 Account findUserBynameWithPwd(String userName, String encStringByUTF8, String accountYes);

	 @Transactional
	 @Modifying
	 @Query(value = "update Account t set t.userPassword=?2 where t.loginId = ?1 ")
	 void updatePwdById(Long loginId, String encStringByUTF8);
	 
	 @Transactional
	 @Query(value = "select t from Account t where t.userName = ?1 ")
	 Account findByUserName(String userName);

	 @Transactional
	 @Query(value = "select t from Account t where t.phone =?1 and t.userPassword = ?2 and t.accountStatus = ?3")
	Account authenticateAppLogin(String userName, String userPwd, String accountYes);
	 
	 @Transactional
	 @Query(value = "select t from Account t where t.phone = ?1 ")
	 Account findByUserPhone(String phone);

}
